local hx__xianjin = fk.CreateSkill {
  name = "hx__xianjin",
  tags = { Skill.Compulsory },
}

local spec = {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__xianjin.name) and player:getMark("hx__xianjin_damage") > 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "hx__xianjin_damage", 0)
    local choices = table.filter({"1", "2", "3"}, function(n)
      return player:getMark("hx__tuoyu"..n) == 0
      end)
    if #choices > 0 then
      local choice = room:askToChoice(player, {
        choices = table.map(choices, function(n) return "hx__tuoyu"..n end),
        skill_name = hx__xianjin.name,
        prompt = "#hx__xianjin-choice",
        detailed = true,
      })
      room:setPlayerMark(player, choice, 1)
    end
    if player:getMark("hx__tuoyu1") == 0 or player:getMark("hx__tuoyu2") == 0 or player:getMark("hx__tuoyu3") == 0 then
      local all_names = Fk:getAllCardNames("t")
      room:askToUseVirtualCard(player, {
        name = all_names,
        skill_name = hx__xianjin.name,
        prompt = "请选择视为使用的牌名",
        cancelable = false,
        extra_data = {
          bypass_times = true,
          extraUse = true,
        },
      })
    end
    if table.every(room.alive_players, function(p)
      return player:getHandcardNum() >= p:getHandcardNum()
    end) then
      player:drawCards(#table.filter({"1", "2", "3"}, function(n) return player:getMark("hx__tuoyu"..n) > 0 end), hx__xianjin.name)
    else
      player:drawCards(player.maxHp, hx__xianjin.name)
    end
  end,
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__xianjin.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "hx__xianjin_damage")
  end,
}

hx__xianjin:addEffect(fk.Damage, spec)
hx__xianjin:addEffect(fk.Damaged, spec)

return hx__xianjin
